<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="roleIds">
      <!-- 选项 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex">
      <el-col :span="12">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetail } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    userId: {
      type: String,
      required: true
    }

  },
  data() {
    return {
      list: [],
      roleIds: []// 选中的id
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    //   角色列表
    async getRoleList() {
      const { rows } = await getRoleList()
      this.list = rows
    },
    // 用户详情
    async getUserDetail(id) {
      const { roleIds } = await getUserDetail(id)
      //   console.log(res)
      this.roleIds = [...roleIds]
    },
    // 点击确定
    async  btnOK() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$message.success('添加角色成功')
      this.$emit('update:showRoleDialog', false)
    },
    // 点击取消
    btnCancel() {
      this.roleIds = []
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>

</style>
